package com.hndzyu.yd.home;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.gyf.barlibrary.ImmersionBar;
import com.scwang.smartrefresh.header.MaterialHeader;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.hndzyu.yd.R;
import com.hndzyu.yd.bean.TaskDescObject;
import com.hndzyu.yd.bean.UserBean;
import com.hndzyu.yd.ui.LoginActivity;
import com.hndzyu.yd.utils.Util;

import java.util.ArrayList;
import java.util.List;

import xcs.ert.jlj.os.PointsChangeNotify;
import xcs.ert.jlj.os.df.AdExtraTaskStatus;
import xcs.ert.jlj.os.df.AdTaskStatus;
import xcs.ert.jlj.os.df.AppDetailDataInterface;
import xcs.ert.jlj.os.df.AppDetailObject;
import xcs.ert.jlj.os.df.AppExtraTaskObject;
import xcs.ert.jlj.os.df.AppExtraTaskObjectList;
import xcs.ert.jlj.os.df.AppSummaryObject;
import xcs.ert.jlj.os.df.DiyAppNotify;
import xcs.ert.jlj.os.df.DiyOfferWallManager;
/**
 *有米详情
 */
public class TaskDetailActivity extends Activity
        implements OnClickListener, DiyAppNotify, PointsChangeNotify {

    public AppSummaryObject appSumObject;

    private ImageView appIcon;

    private TextView rewardCount;

    private TextView appName;

    private TextView appVersion;

    private TextView appSize;

    private TextView appStyle;

    private Button openOrDownloadBtn;

    private ProgressBar downlaodProgressBar;

    private SmartRefreshLayout refresh; // 下拉刷新组件

    private ListView listView;

    private ListViewAdapter_TaskDesc lvAdapter;

    private AppDetailObject appDetailObject;


    /**
     * 任务描述列表的数据列表
     */
    private ArrayList<TaskDescObject> mTaskDescList;

    private boolean isPackageExist = false;

    private final static Handler handler = new Handler();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.acticity_taskdetail);
        ImmersionBar.with(this).statusBarColor(R.color.them)
                .init();
        // 检查传入的Intent是否合法，不合法就直接finish
        Object obj = getIntent().getSerializableExtra("ad");
        if (obj == null || !(obj instanceof AppSummaryObject)) {
            this.finish();
            return;
        }

        appSumObject = (AppSummaryObject) obj;
        // 检查这个应用是否已经存在于手机中
        isPackageExist = Util.isPackageExist(this, appSumObject.getPackageName());

        // 初始化View
        initView();


        // （可选）注册广告下载安装监听-随时随地获得应用下载安装状态的变动情况
        DiyOfferWallManager.getInstance(this).registerListener(this);

        // 获取广告的详细数据
        requestDetailData();

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        // （可选）注销下载安装监听-如果在onCreate中注册了，那这里必须得注销
        DiyOfferWallManager.getInstance(this).removeListener(this);
        ImmersionBar.with(this).destroy(); //必须调用该方法，防止内存泄漏
    }

    private void initView() {
        refresh = findViewById(R.id.refresh);
        refresh.setRefreshHeader(new MaterialHeader(this));
        refresh.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(RefreshLayout refreshlayout) {
                refreshlayout.finishRefresh(2000/*,false*/);//传入false表示刷新失败
                requestDetailData();
            }
        });
        findViewById(R.id.backarea).setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        TextView total = findViewById(R.id.tv_detailpage_total);
        try {
        total.setText("+" + Util.Sub((double) getTotalPoints(appSumObject) * Double.parseDouble(UserBean.divide) / 100 + "") + "元");
        TextView vipdivide = findViewById(R.id.tv_detailpage_vip);
        vipdivide.setText( Util.Sub((double) getTotalPoints(appSumObject) * (Double.parseDouble(UserBean.vipdivide)+ Double.parseDouble(UserBean.divide))/ 100 + "")+ "元");
        }catch (Exception e){finish();}

        appIcon = (ImageView) findViewById(R.id.iv_detailpage_appicon);
        appName = (TextView) findViewById(R.id.tv_detailpage_appname);
        appVersion = (TextView) findViewById(R.id.tv_detailpage_apppvn);
        appSize = (TextView) findViewById(R.id.tv_detailpage_appsize);
//        appStyle = (TextView) findViewById(R.id.tv_detailpage_appstyle);

        rewardCount = (TextView) findViewById(R.id.tv_detailpage_rewards_count);
        findViewById(R.id.backarea).setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        downlaodProgressBar = (ProgressBar) findViewById(R.id.pb_download);
        downlaodProgressBar.setVisibility(View.GONE);

        openOrDownloadBtn = (Button) findViewById(R.id.btn_detailpage_open_or_install);
        openOrDownloadBtn.setVisibility(View.INVISIBLE);
        openOrDownloadBtn.setOnClickListener(this);
        updateOpenOrDownloadButtonStatus(appSumObject.getAdTaskStatus());


        listView = (ListView) findViewById(R.id.detailpage_listview);
        listView.setEnabled(false);
        lvAdapter = new ListViewAdapter_TaskDesc(this, null);
        listView.setAdapter(lvAdapter);
    }


    @Override
    public void onPointBalanceChange(float points) {

    }

    /**
     * 获取广告的详细数据,下面展示两种加载方式，开发者可选择适合自己的方式
     */
    private void requestDetailData() {


        // 异步加载方式
        DiyOfferWallManager.getInstance(this)
                .loadAppDetailData(appSumObject, new AppDetailDataInterface() {

                    /**
                     * 当成功请求到数据的时候，会回调本方法（注意:本接口不在UI线程中执行， 所以请不要在本接口中进行UI线程方面的操作） 注意：广告详细数据有可能为空，开发者处理之前，请先判断是否为空
                     */
                    @Override
                    public void onLoadAppDetailDataSuccess(Context context, AppDetailObject appDetailObject) {
                        updateView(appDetailObject);
                    }

                    /**
                     * 请求成功，但是返回错误代码时候，会回调这个接口
                     */
                    @Override
                    public void onLoadAppDetailDataFailedWithErrorCode(int code) {
                        notifyRequestFailed("请求错误，错误代码 ： %d， 请联系客服", code);
                    }

                    /**
                     * 因为网络问题而导致请求失败时，会回调这个接口（注意:本接口不在UI线程中执行， 所以请不要在本接口中进行UI线程方面的操作）
                     */
                    @Override
                    public void onLoadAppDetailDataFailed() {
                        notifyRequestFailed("请求失败，请检查网络");
                    }
                });

        //		// 同步加载方式
        //		new Thread(new Runnable() {
        //			@Override
        //			public void run() {
        //				try {
        //					AppDetailObject data = DiyOfferWallManager.getInstance(OfferWallAdDetailActivity
        //							.this).getAppDetailData(mWeChatAddFensAdObject);
        //					updateView(data);
        //				} catch (NetworkException e) {
        //					Log.e("___Sdk", "", e);
        //					notifyRequestFailed("请求失败，请检查网络");
        //				} catch (ErrorCodeException e) {
        //					Log.e("___Sdk", "", e);
        //					notifyRequestFailed("请求错误，错误代码 ： %d， 请联系客服", e.getErrCode());
        //				}
        //			}
        //		}).start();
    }

    private void notifyRequestFailed(final String format, final Object... args) {
        handler.post(new Runnable() {

            @Override
            public void run() {
//				mSwipeRefreshLayout.setRefreshing(false);
                new AlertDialog.Builder(getApplicationContext()).setTitle("请求失败").setMessage(String.format(format, args))
                        .create().show();
            }
        });
    }

    /**
     * 更新按钮状态
     */
    private void updateOpenOrDownloadButtonStatus(int status) {
        switch (status) {
            case AdTaskStatus.NOT_COMPLETE: // 未完成
                openOrDownloadBtn.setEnabled(true);
                openOrDownloadBtn.setText(isPackageExist ? "任务未完成，打开体验" : "下载安装");
                break;
            case AdTaskStatus.HAS_EXTRA_TASK: // 有追加任务
                openOrDownloadBtn.setEnabled(true);
                boolean isExtraTaskCanDo = false; // 标记追加任务现在是否可以进行
                for (int i = 0; i < appSumObject.getExtraTaskList().size(); ++i) {
                    if (AdExtraTaskStatus.IN_PROGRESS ==
                            appSumObject.getExtraTaskList().get(i).getStatus()) {
                        isExtraTaskCanDo = true;
                        break;
                    }
                }
                openOrDownloadBtn.setText(isPackageExist ? (isExtraTaskCanDo ? "任务未完成，打开体验" : "任务等待中") : "下载安装");
                break;
            case AdTaskStatus.ALREADY_COMPLETE: // 已完成
                openOrDownloadBtn.setEnabled(true);
                openOrDownloadBtn.setText(isPackageExist ? "任务已完成，打开" : "重新安装");
                break;
            default:
                break;
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_detailpage_open_or_install:
                if (UserBean.uid == 0) {
                    startActivity(new Intent(TaskDetailActivity.this, LoginActivity.class));
                } else {
                    if (appDetailObject != null) {

                        // 如果应用已经安装了，就统一使用sdk的打开api
                        // 如果应用还没有安装的话,需要判断选用哪种下载方式，
                        // 1、调用sdk的下载api进行下载（适用于国内app）
                        // 2、跳转到googleplay进行下载（使用与国外app）
                        if (!isPackageExist) {

                            // 如果googleplay的跳转url不为空，就用用外部浏览器打开googleplay的url进行跳转
                            String googlePlayUrl = appDetailObject.getGoogleAdDownloadUrl();
                            if (googlePlayUrl != null && googlePlayUrl.trim().length() > 0) {
                                Util.tryToOpenGooglePlayToDownload(this, googlePlayUrl);
                                break;
                            }
                        }
                        DiyOfferWallManager.getInstance(this).openOrDownloadApp(this, appDetailObject);
                    }
                }
                break;

            default:
                break;
        }
    }

    /**
     * 构造任务描述列表
     *
     * @return
     */
    private void generateDestList() {
        if (mTaskDescList == null) {
            mTaskDescList = new ArrayList<TaskDescObject>();
        }

        // 1、将正常的任务加入到描述列表中
        int status = 0;
        if (appDetailObject.getAdTaskStatus() == AdTaskStatus.ALREADY_COMPLETE ||
                appDetailObject.getAdTaskStatus() == AdTaskStatus.HAS_EXTRA_TASK) {
            status = AdExtraTaskStatus.COMPLETE; // 标记任务已经完成
        } else if (appDetailObject.getAdTaskStatus() == AdTaskStatus.NOT_COMPLETE) {
            status = AdExtraTaskStatus.IN_PROGRESS; // 标记任务可以进行
        }
        TaskDescObject normalTask =
                new TaskDescObject(status, appDetailObject.getTaskSteps(), appDetailObject.getPoints());
        mTaskDescList.add(normalTask);

        // 2、将追加任务加入到描述列表中
        AppExtraTaskObjectList extraTastkList = appDetailObject.getExtraTaskList();
        if (extraTastkList != null && extraTastkList.size() > 0) {
            for (int i = 0; i < extraTastkList.size(); ++i) {
                AppExtraTaskObject extraTaskObject = extraTastkList.get(i);
                TaskDescObject temp =
                        new TaskDescObject(extraTaskObject.getStatus(), extraTaskObject.getAdText(),
                                extraTaskObject.getPoints());
                mTaskDescList.add(temp);
            }
        }
    }

    /**
     * 更新视图
     *
     * @param
     */
    private void updateView(final AppDetailObject detailData) {
        if (detailData != null) {

            this.appDetailObject = detailData;

            // 这里生成一下描述列表
            generateDestList();


            // 在主线程中更新数据
            handler.post(new Runnable() {

                @Override
                public void run() {
                    openOrDownloadBtn.setVisibility(View.VISIBLE);
                    appName.setText(appDetailObject.getAppName());
                    appVersion.setText("版本号：" + appDetailObject.getVersionName());
                    appSize.setText("大小：" + appDetailObject.getAppSize());
//                    appStyle.setText(appDetailObject.getAppCategory());
                    updateOpenOrDownloadButtonStatus(appDetailObject.getAdTaskStatus());
                    updateListView(mTaskDescList);
                    refresh.finishRefresh(false);
                    Glide.with(getApplicationContext()).load(appDetailObject.getIconUrl()).apply(new RequestOptions().placeholder(R.mipmap.logo)).into(appIcon);
                    rewardCount.setText("今天已有 " + appDetailObject.getRewardsCount() + " 个用户获得奖励");
                }
            });
//
//            // 构造需要加载的图片url数组，当图片加载完毕的时候更新显示
//            // 1、传入图标url
//            int ssUrlsLength = appDetailObject.getScreenShotUrls().length;
//            String[] imageUrlArray = new String[ssUrlsLength + 1];
//            imageUrlArray[0] = appDetailObject.getIconUrl();


//			// 2、传入截图url
//			if (appDetailObject.getScreenShotUrls() != null) {
//				System.arraycopy(appDetailObject.getScreenShotUrls(), 0, imageUrlArray, 1, ssUrlsLength); // 传入截图地址
//			}

            // 线程池异步加载图片
//			BitmapLoaderManager.loadBitmap(this, this, imageUrlArray);
        }
    }

    /**
     * 更新listview显示任务描述
     *
     * @param
     */
    private void updateListView(List<TaskDescObject> list) {
        if (list != null && !list.isEmpty()) {
            listView.setVisibility(View.VISIBLE);
            lvAdapter.setData(list);
            lvAdapter.notifyDataSetChanged();
        } else {
            listView.setVisibility(View.GONE);
        }
    }

    @Override
    public void onDownloadStart(int id) {

    }

    @Override
    public void onDownloadFailed(int id) {
        try {
            if (appDetailObject == null) {
                return;
            }

            if (appDetailObject.getAdId() != id) {
                return;
            }
            this.downlaodProgressBar.setProgress(0);
            this.downlaodProgressBar.setVisibility(View.GONE);
            this.openOrDownloadBtn.setEnabled(true);
            this.openOrDownloadBtn.setText("下载失败,请稍候重试!");
        } catch (Throwable e) {
            Log.d("___", "", e);
        }
    }

    @Override
    public void onDownloadSuccess(int id) {
        try {
            if (appDetailObject == null) {
                return;
            }

            if (appDetailObject.getAdId() != id) {
                return;
            }
            this.downlaodProgressBar.setProgress(0);
            this.downlaodProgressBar.setVisibility(View.GONE);
            this.openOrDownloadBtn.setEnabled(true);
            this.openOrDownloadBtn.setText("下载成功,请安装!");
        } catch (Throwable e) {
            Log.d("___", "", e);
        }
    }

    @Override
    public void onDownloadProgressUpdate(int id, long contentLength, long completeLength, int percent, long speedBytesPerS) {
        try {
            if (appDetailObject == null) {
                return;
            }

            if (appDetailObject.getAdId() != id) {
                return;
            }
            this.downlaodProgressBar.setVisibility(View.VISIBLE);
            this.downlaodProgressBar.setProgress(percent);
            this.openOrDownloadBtn.setEnabled(false);
            this.openOrDownloadBtn.setText(String.format("正在下载,已完成%d%% ,下载速度: %dKB/s", percent, (speedBytesPerS / 1024)));
        } catch (Throwable e) {
            Log.d("___", "", e);
        }

    }

    @Override
    public void onInstallSuccess(int id) {
        try {
            if (appDetailObject == null) {
                return;
            }

            if (appDetailObject.getAdId() != id) {
                return;
            }
            this.downlaodProgressBar.setProgress(0);
            this.downlaodProgressBar.setVisibility(View.GONE);
            this.openOrDownloadBtn.setEnabled(true);
            this.openOrDownloadBtn.setText("已安装成功,打开");
        } catch (Throwable e) {
            Log.d("___", "", e);
        }
    }

    /**
     * 如果任务未完成就获取指定广告的所有积分（正常完成的积分+可完成的追加任务积分）
     */
    private int getTotalPoints(AppSummaryObject appSummaryObject) {
        int totalpoints = appSummaryObject.getPoints();
        AppExtraTaskObjectList tempList = appSummaryObject.getExtraTaskList();
        if (tempList != null && tempList.size() > 0) {
            for (int i = 0; i < tempList.size(); ++i) {
                AppExtraTaskObject extraTaskObject = tempList.get(i);
                if (extraTaskObject.getStatus() == AdExtraTaskStatus.NOT_START ||
                        extraTaskObject.getStatus() == AdExtraTaskStatus.IN_PROGRESS) {
                    totalpoints += extraTaskObject.getPoints();
                }
            }
        }
        return totalpoints;
    }


}
